package com.video.video.service.impl;

import com.video.common.exception.ServiceException;
import com.video.common.utils.uuid.IdUtils;
import com.video.video.domain.DUser;
import com.video.video.domain.app.dto.AppEchargeRecordDto;
import com.video.video.domain.dto.DEchargeRecordDto;
import com.video.video.domain.vo.EchargeQuery;
import com.video.video.service.IDUserService;
import java.util.List;
import com.video.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.video.video.mapper.DEchargeRecordMapper;
import com.video.video.domain.DEchargeRecord;
import com.video.video.service.IDEchargeRecordService;

/**
 * 充值记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-27
 */
@Service
public class DEchargeRecordServiceImpl implements IDEchargeRecordService
{
    @Autowired
    private DEchargeRecordMapper dEchargeRecordMapper;
    @Autowired
    private IDUserService dUserService;

    /**
     * 查询充值记录
     *
     * @param id 充值记录主键
     * @return 充值记录
     */
    @Override
    public DEchargeRecord selectDEchargeRecordById(String id)
    {
        return dEchargeRecordMapper.selectDEchargeRecordById(id);
    }

    @Override
    public List<DEchargeRecordDto> getEchargeRecordList(String userId) {
        return dEchargeRecordMapper.getEchargeRecordList(userId);
    }

    /**
     * 查询充值记录列表
     *
     * @param query 充值记录
     * @return 充值记录
     */
    @Override
    public List<DEchargeRecordDto> selectDEchargeRecordList(EchargeQuery query)
    {
        return dEchargeRecordMapper.selectDEchargeRecordList(query);
    }

    /**
     * 新增充值记录
     *
     * @param dEchargeRecord 充值记录
     * @return 结果
     */
    @Override
    public int insertDEchargeRecord(DEchargeRecord dEchargeRecord)
    {
        dEchargeRecord.setId(IdUtils.getId());
        dEchargeRecord.setCreateTime(DateUtils.getNowDate());
        DUser user=dUserService.selectDUserById(dEchargeRecord.getUid());
        if(user == null){
            throw new ServiceException("用户不存在");
        }
        dEchargeRecord.setCurrentAmount(user.getBalance());         //当前会员余额金额
        dEchargeRecord.setCurrentGiveAmount(user.getGiveBalance()); //当前会员赠送金额的余额

        int flg= dEchargeRecordMapper.insertDEchargeRecord(dEchargeRecord);
        if(flg > 0){
            user.setBalance(user.getBalance().add(dEchargeRecord.getAmount()));

            //累加用户表中的 赠送金额的余额
            user.setGiveBalance(user.getGiveBalance().add(dEchargeRecord.getGiveAmount()));
            dUserService.updateDUserInfo(user);
        }

        return flg;
    }

    /**
     * 修改充值记录
     *
     * @param dEchargeRecord 充值记录
     * @return 结果
     */
    @Override
    public int updateDEchargeRecord(DEchargeRecord dEchargeRecord)
    {
        dEchargeRecord.setUpdateTime(DateUtils.getNowDate());
        return dEchargeRecordMapper.updateDEchargeRecord(dEchargeRecord);
    }

    /**
     * 批量删除充值记录
     *
     * @param ids 需要删除的充值记录主键
     * @return 结果
     */
    @Override
    public int deleteDEchargeRecordByIds(String[] ids)
    {
        return dEchargeRecordMapper.deleteDEchargeRecordByIds(ids);
    }

    /**
     * 删除充值记录信息
     *
     * @param id 充值记录主键
     * @return 结果
     */
    @Override
    public int deleteDEchargeRecordById(String id)
    {
        return dEchargeRecordMapper.deleteDEchargeRecordById(id);
    }
}
